/*
 * 使用示例:
 * const logger = require('index.js')(options)(type);
 * 
 * options支持的参数:
 * logPath 日志文件目录
 * logLevel 记录的日志类型
 * 
 * type参数:日志类别(该类别即保存的日志文件名)
 */
'use strict';

var fs = require('fs');
var path = require('path');
var moment = require('moment');
var winston = require('winston');
var DailyRotateFile = require('winston-daily-rotate-file');

exports = module.exports = (options) => {
  fs.existsSync(options.logPath) || fs.mkdirSync(options.logPath);
  return (type) => {
    type = type || 'request';

    return new winston.Logger({
      level: options.logLevel,
      transports: [
        new(winston.transports.Console)(),
        new DailyRotateFile({
          filename: path.join(options.logPath, type),
          datePattern: '-yyyyMMdd.log',
          timestamp: function() {
            return moment().format('YYYY-MM-DD HH:mm:ss');
          },
          json: false
        })
      ],
      exitOnError: false
    });
  };
};